package com.lxk.mall.pms.controller.app;

import cn.hutool.core.collection.CollUtil;
import com.lxk.mall.pms.controller.app.vo.AppProductBrowseHistoryDeleteReqVO;
import com.lxk.mall.pms.controller.app.vo.AppProductBrowseHistoryPageReqVO;
import com.lxk.mall.pms.controller.app.vo.AppProductBrowseHistoryRespVO;
import com.lxk.mall.pms.controller.app.vo.ProductBrowseHistoryPageReqVO;
import com.lxk.mall.pms.domain.PmsProductBrowseHistory;
import com.lxk.mall.pms.domain.PmsProductSpu;
import com.lxk.mall.pms.service.IPmsProductBrowseHistoryService;
import com.lxk.mall.pms.service.IPmsProductSpuService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.dromara.common.core.utils.CollectionUtils.convertMap;
import static org.dromara.common.core.utils.CollectionUtils.convertSet;


/**
 * 商品浏览记录
 */
@RestController
@RequestMapping("/product/browse-history")
@RequiredArgsConstructor
public class AppProductBrowseHistoryController {

    private final IPmsProductBrowseHistoryService productBrowseHistoryService;

    private final IPmsProductSpuService productSpuService;

    /**
     * 删除商品浏览记录
     * @param reqVO
     * @return
     */
    @DeleteMapping(value = "/delete")
    public R<Boolean> deleteBrowseHistory(@RequestBody @Valid AppProductBrowseHistoryDeleteReqVO reqVO) {
        productBrowseHistoryService.hideUserBrowseHistory(LoginHelper.getUserId(), reqVO.getSpuIds());
        return R.ok(Boolean.TRUE);
    }

    /**
     * 清空商品浏览记录
     * @return
     */
    @DeleteMapping(value = "/clean")
    public R<Boolean> deleteBrowseHistory() {
        productBrowseHistoryService.hideUserBrowseHistory(LoginHelper.getUserId(), null);
        return R.ok(Boolean.TRUE);
    }

    /**
     * 商品浏览记录分页
     *
     * @param reqVO
     * @return
     */
    @GetMapping(value = "/page")
    public TableDataInfo<AppProductBrowseHistoryRespVO> getBrowseHistoryPage(AppProductBrowseHistoryPageReqVO reqVO) {
        ProductBrowseHistoryPageReqVO pageReqVO = new ProductBrowseHistoryPageReqVO();
        org.springframework.beans.BeanUtils.copyProperties(reqVO, pageReqVO);
        pageReqVO.setUserId(LoginHelper.getUserId());
        pageReqVO.setUserDeleted(false);
        TableDataInfo<PmsProductBrowseHistory> pageResult = productBrowseHistoryService.getBrowseHistoryPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getRows())) {
            return TableDataInfo.build();
        }
        // 得到商品 spu 信息
        Set<Long> spuIds = convertSet(pageResult.getRows(), PmsProductBrowseHistory::getSpuId);
        Map<Long, PmsProductSpu> spuMap = convertMap(productSpuService.getSpuList(spuIds), PmsProductSpu::getId);
        List<AppProductBrowseHistoryRespVO> voList = pageResult.getRows().stream().map(item -> {
            AppProductBrowseHistoryRespVO vo = new AppProductBrowseHistoryRespVO();
            BeanUtils.copyProperties(item, vo);

            if (CollUtil.isNotEmpty(spuMap) && spuMap.get(vo.getSpuId()) != null) {
                PmsProductSpu spu = spuMap.get(vo.getSpuId());
                vo.setSpuName(spu.getName());
                vo.setPicUrl(spu.getPicUrl());
                vo.setPrice(spu.getPrice());
            }
            return vo;
        }).collect(Collectors.toList());
        TableDataInfo<AppProductBrowseHistoryRespVO> tableDataInfo = TableDataInfo.build(voList);
        tableDataInfo.setTotal(pageResult.getTotal());
        return tableDataInfo;
    }

}
